// pages/personal/personal.js
var app = getApp();
function ageNum(){
	var agelistArray = [];
	for(var i = 18 ; i<=65; i++){
		agelistArray.push(i);
	}
	return agelistArray;
}
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		genderlist: ['男', '女'],//性别
		genderindex: 0,//性别index
		agelist:[],
		ageindex: 10,
		multiArray: [],
		multiIndex: [0, 0],

		personal: {
			avatarUrl: '/images/small_avatar.png',
		},//个人信息对象
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			agelist: ageNum()
		})
		app.loading();
		this.getCityLv();
		
	},
	//获取地区城市
	getCityLv: function () {
		var that = this;
		app.apiGet(app.apiList.cityslv, {}, function (res) {
			if (res.code == 200) {
				var data = res.data, multiArrayObj = [], parentObj = [], childObj = [];
				for (var i = 0; i < data.length; i++) {
					parentObj.push(data[i]);
				}
				var defData = data[0].sub;
				for (var j = 0; j < defData.length; j++) {
					childObj.push(defData[j])
				}
				multiArrayObj.push(parentObj);
				multiArrayObj.push(childObj);
				that.setData({
					cityData: data,
					multiArray: multiArrayObj
				})
				that.getPersonal();
			}
		})
	},
	//获取个人信息
	getPersonal: function () {
		var that = this;
		app.apiGet(app.apiList.getPersonal, {}, function (res) {
			app.hideloading();
			if (res.code == 200) {
				var multiIndex=[];
				var multiArray=[];
				multiArray[0] = that.data.multiArray[0];
				var cityData = that.data.cityData;
				if (res.data.province == '' || res.data.city==''){
					for (var i = 0; i < cityData.length; i++) {
						if (cityData[i].title.indexOf(app.globalData.userInfo.province) != -1) {
							multiIndex[0] = i;
							for (var j = 0; j < cityData[i].sub.length; j++) {
								if (cityData[i].sub[j].title.indexOf(app.globalData.userInfo.city) != -1) {
									multiIndex[1] = j;
									multiArray[1] = cityData[i].sub;
								}
							}
						}
					}
				}else{
					for (var i = 0; i < cityData.length; i++) {
						if (res.data.province.id == cityData[i].id) {
							multiIndex[0] = i;
							for (var j = 0; j < cityData[i].sub.length; j++) {
								if (res.data.city.id == cityData[i].sub[j].id) {
									multiIndex[1] = j;
									multiArray[1] = cityData[i].sub;
								}
							}
						}
					}
				}
				
				that.setData({
					personal: res.data,
					genderindex: res.data.sex,
					ageindex: res.data.age !='' ? res.data.age - 18 : that.data.ageindex,
					multiIndex: multiIndex,
					multiArray: multiArray
				})
			}
		})
	},
	//上传图片
	uploadImgTap: function () {
		var that = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
				var tempFilePaths = res.tempFilePaths[0];
				var tempFiles = res.tempFiles[0];
				//图片不能大于2M
				if (tempFiles.size >= 2097152) {
					app.alert('图片大小不能超过2M！');
				} else {
					that.setData({
						'personal.avatarUrl': tempFilePaths,
						avatarUrlStaus: true
					})
				}
			}
		})
	},
	//姓名
	nickNameTap: function (e) {
		this.data.personal.nickName = e.detail.value
	},
	//性别
	bindPickerChangeSex: function (e) {
		this.data.personal.sex = e.detail.value
		this.setData({
			genderindex: e.detail.value
		})
	},
	//年龄
	bindPickerChangeAge: function (e) {
		this.data.personal.age = Number(e.detail.value) + Number(18);
		this.setData({
			ageindex: e.detail.value
		})
	},
	//地区
	bindMultiPickerChange: function (e) {
		console.log('picker发送选择改变，携带值为', e.detail.value)
		var valArray  = e.detail.value;
		var province_id = this.data.multiArray[0][valArray[0]].id;
		var province_title =  this.data.multiArray[0][valArray[0]].title;
		var city_id = this.data.multiArray[1][valArray[1]].id;
		var city_title = this.data.multiArray[1][valArray[1]].title;
		this.setData({
			multiIndex: valArray,
			'personal.province_id': province_id,
			'personal.city_id': city_id,
			'personal.province.title': province_title,
			'personal.city.title': city_title,
		})
	},
	bindMultiPickerColumnChange: function (e) {
		console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
		if (e.detail.column == 0) {
			var defData = this.data.cityData[e.detail.value].sub;
			var childObj = [], multiArrayObj = [];
			for (var j = 0; j < defData.length; j++) {
				childObj.push(defData[j])
			}

			this.data.multiArray = [this.data.multiArray[0], childObj];
			this.data.multiIndex = [e.detail.value, 0];
			this.setData({
				multiArray: this.data.multiArray,
				multiIndex: this.data.multiIndex
			})
		}

	},
	//真实姓名
	realNameTap: function (e) {
		this.data.personal.realName = e.detail.value
	},
	//手机号码
	phoneNumberTap: function (e) {
		this.data.personal.phoneNumber = e.detail.value
	},
	//工作邮箱
	emailTap: function (e) {
		this.data.personal.email = e.detail.value
	},
	//所属岗位
	jobTap: function (e) {
		this.data.personal.job = e.detail.value
	},
	//公司名称
	companyTap: function (e) {
		this.data.personal.company = e.detail.value
	},

	//保存
	submitTap: function () {

		var that = this;
		if (this.data.personal.nickName == '' || this.data.personal.nickName == undefined) {
			app.alert('昵称不能为空！');
			return false;
		}
		var phoneNumber = this.data.personal.phoneNumber;
		if (phoneNumber != '' && phoneNumber != undefined && !(/^1(3|4|5|7|8)\d{9}$/.test(phoneNumber))) {
			app.alert('手机号码格式不对！');
			return false;
		}
		var email = this.data.personal.email;
		var str = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(email);
		if (email != '' && email != undefined && !str) {
			app.alert('邮箱格式不对！');
			return false;
		}
		app.loading();
		
		if (this.data.avatarUrlStaus){
			app.util.uploadImage(this.data.personal.avatarUrl, function (res) {
				that.data.personal.avatar = res.data.path;
				that.data.personal.avatarUrl = res.data.url;
				that.uploadCollectionsFun();
			})
		}else{
			that.uploadCollectionsFun();
		}
		
	},
	//修改个人信息
	uploadCollectionsFun: function (){
		var that = this;
		app.apiPost(app.apiList.editPersonal, this.data.personal, function (res) {
			app.hideloading();
			if (res.code == 200) {
				wx.showToast({
					title: '提交成功！',
					icon: 'success',
					duration: 1000,
					mask: true,
					success: function () {

						var pages = getCurrentPages();
						var curPage = pages[pages.length - 2];
						var userInfo = {
							nickName: that.data.personal.nickName,
							gender: that.data.personal.sex,
							city: that.data.personal.city.title,
							province: that.data.personal.province.title,
							avatarUrl: that.data.personal.avatarUrl
						}
						curPage.setData({
							userInfo: userInfo,
						});
						wx.setStorage({
							key: "userInfo",
							data: userInfo
						})
						app.globalData.userInfo = userInfo;
						wx.navigateBack({

						})
					}
				})

			}
		})
	},
	//share 统一分享
	onShareAppMessage: function (res) {
		if (res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '财咖｜会计招聘找工作',
			path: '/pages/index/index',
			imageUrl: '/images/share.jpg',
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			}
		}
	},



})